home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
PET
/
S-Super PET
/
(s)t4.d64
/
KNIGHTS_TOUR.BAS
< prev
next >
Wrap
BASIC Source File
|
2009-01-18
|
2KB
|
92 lines
10 ! Knight's Tour
20 ! by Jack Schueler
30 ! Waterloo Computing Systems Limited
40 ! Waterloo, Ontario, Canada
50
60 ! This program attempts to find a tour of the chess board
70 ! using the knight's move to cover all squares once and
80 ! only once. The algorithm used is a heuristic. Given a
90 ! sufficient amount of time (sometimes megayears) it will
100 ! find a solution. Fortunately solutions to some starting
110 ! positions are found fairly quickly. Try 4,3; it's quick.
120
130 dim istack%(64),jstack%(64),dstack%(64),board%(11,11)
140 dim di%(11),dj%(11)
150 data 2, 1,-1,-2,-2,-1, 1, 2, 2, 1,-1,-2
160 data -1,-2,-2,-1, 1, 2, 2, 1,-1,-2,-2,-1
170 mat read di%, dj%
180 mat board% = (-1)
190 for i% = 2 to 9
200 for j% = 2 to 9
210 board%(i%,j%) = 0
220 next j%
230 next i%
240 print 'Enter the starting position (1,1 is the top left corner)'
250 input i%, j%
260 call drawboard
270 i% = i% + 1
280 j% = j% + 1
290 d% = -1
300 sp% = 1
310 loop
320 offset% = 4 * (i% <= j%)
330 guess
340 loop
350 d% = d% + 1
360 if d% = 8 then 500
370 ti% = i% + di%(d% + offset%)
380 tj% = j% + dj%(d% + offset%)
390 until board%(ti%,tj%) = 0
400 istack%(sp%) = i%
410 jstack%(sp%) = j%
420 dstack%(sp%) = d%
430 board%(i%,j%) = sp%
440 x$=fnp$(i%,j%,sp%)
450 i% = ti%
460 j% = tj%
470 sp% = sp% + 1
480 d% = -1
490 admit
500 sp% = sp% - 1
510 board%(i%,j%) = 0
520 x$=fnp$(i%,j%,0)
530 i% = istack%(sp%)
540 j% = jstack%(sp%)
550 d% = dstack%(sp%)
560 endguess
570 until sp% = 64
580 board%(i%,j%) = 64
590 x$=fnp$(i%,j%,64)
600 x$=fnw$(0,23,"")
610 x=cursor(1)
620 stop
630 proc drawboard
640 print chr$(12)
650 x$=rpt$("! ",8)+"!"
660 y$=rpt$("-",49)
670 for row% = 0 to 22
680 if mod(row%,3) <> 2
690 z$=fnw$(12,row%,x$)
700 else
710 z$=fnw$(12,row%,y$)
720 endif
730 next row%
740 endproc
750 def fnw$( col%, row%, ch$ )
760 pos% = row%*80 + col% + 1
770 pos=cursor(pos%)
780 print ch$;
790 fnw$=ch$
800 fnend
810 def fnp$( i%, j%, s% )
820 i% = i% - 2
830 j% = j% - 2
840 if s% = 0
850 x$=fnw$(14+6*j%, 3*i%, " ")
860 else
870 x$=fnw$(14+6*j%, 3*i%, value$(s%) )
880 endif
890 fnp$=""
900 fnend
910 end